package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 新增套餐,添加菜品
     *
     * @param setmealDto
     * @return
     */
    @Override
    @CacheEvict(value = "setmealCache", allEntries = true)
    public SetmealDto addSetmeal(SetmealDto setmealDto) {
        //添加套餐
        setmealMapper.insert(setmealDto);
        //给套餐加菜品
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();

        setmealDishList.stream().map(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            return setmealDish;
        }).forEach(setmealDish -> setmealDishMapper.insert(setmealDish));
        redisTemplate.opsForSet().add("set2",setmealDto.getImage());
        return setmealDto;
    }

    /**
     * 分页条件查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetmealDto> getSetmealPage(int page, int pageSize, String name) {
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        setmealMapper.selectPage(setmealPage, lqw);
        Page<SetmealDto> setmealDtoPage = new Page<>();

        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");
        List<Setmeal> setmealPageRecords = setmealPage.getRecords();

        List<SetmealDto> setmealDtoList = setmealPageRecords.stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            Long categoryId = setmeal.getCategoryId();

            BeanUtils.copyProperties(setmeal, setmealDto);

            Category category = categoryMapper.selectById(categoryId);
            setmealDto.setCategoryName(category.getName());

            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(setmealDtoList);
        return setmealDtoPage;
    }

    /**
     * 删除套餐
     *
     * @param idList
     */
    @Override
    @CacheEvict(value = "setmealCache", allEntries = true)
    public void deleteSetmeal(List<Long> idList) {
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.in(SetmealDish::getSetmealId, idList);
        setmealDishMapper.delete(lqw);
        //删除套餐
        List<Setmeal> setmeals = setmealMapper.selectBatchIds(idList);
        for (Setmeal setmeal : setmeals) {
            Integer status = setmeal.getStatus();
            if (status == 1) {
                throw new CustomException("菜品正在销售");
            }
        }
        setmealMapper.deleteBatchIds(idList);
    }

    /**
     * 修改套餐状态(启售,禁售)
     *
     * @param status
     * @param idList
     */
    @Override
    @CacheEvict(value = "setmealCache", allEntries = true)
    public void setmealStatusByStatus(Integer status, List<Long> idList) {
        idList.stream().map(item -> {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(item);
            setmeal.setStatus(status);
            return setmeal;
        }).forEach(setmeal -> setmealMapper.updateById(setmeal));
    }

    /**
     * 修改后保存
     *
     * @param setmealDto
     */
    @Override
    @CacheEvict(value = "setmealCache", allEntries = true)
    public void editSetmeal(SetmealDto setmealDto) {
        setmealMapper.updateById(setmealDto);
        Long id = setmealDto.getId();
        //删除套餐中的菜品信息
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId, id);
        setmealDishMapper.delete(lqw);
        //重新添加菜品信息
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        setmealDishList.stream().map(setmealDish -> {
            setmealDish.setSetmealId(id);
            return setmealDish;
        }).forEach(setmealDishMapper::insert);
    }

    /**
     * 修改回显数据
     *
     * @param id
     * @return
     */
    @Override
    public SetmealDto querySetmealById(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        SetmealDto setmealDto = new SetmealDto();
        //克隆
        BeanUtils.copyProperties(setmeal, setmealDto);
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(lqw);
        setmealDto.setSetmealDishes(setmealDishList);
        return setmealDto;
    }

    @Override
    //
    @Cacheable(value = "setmealCache", key = "'setmeal_'+#setmeal.categoryId+'_'+#setmeal.status")
    public List<Setmeal> queryDishList(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        lqw.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        lqw.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmeals = setmealMapper.selectList(lqw);
        return setmeals;
    }

    @Override
    //查看套餐中的菜品及价格
    @Cacheable(value = "setmealCache", key = "'setmeal_'+#setmealId")
    public List<SetmealDish> setMealDishDetails(Long setmealId) {
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId, setmealId);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(lqw);
        Dish dish;
        for (SetmealDish setmealDish : setmealDishList) {
            Long dishId = setmealDish.getDishId();
            dish = dishMapper.selectById(dishId);
            String image = dish.getImage();
            setmealDish.setImage(image);
        }
        return setmealDishList;
    }
}
